package com.fr.data.condition;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.list.IntList;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.report.cellElement.Formula;
import com.fr.report.core.ScriptUtils;
import com.fr.report.script.Calculator;
import com.fr.report.script.CurrentValueNameSpace;
import com.fr.report.script.SingleRowNameSpace;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FormulaCondition extends AbstractCondition
{
  private String formulaContent;

  public FormulaCondition()
  {
  }

  public FormulaCondition(String paramString)
  {
    this.formulaContent = paramString;
  }

  public String getFormula()
  {
    return this.formulaContent;
  }

  public void setFormula(String paramString)
  {
    this.formulaContent = paramString;
  }

  public String[] getDependence()
  {
    return ScriptUtils.getDependenceByFormula(this.formulaContent);
  }

  public boolean eval(Object paramObject, Calculator paramCalculator)
  {
    if (paramObject instanceof Formula)
      paramObject = ((Formula)paramObject).getResult();
    CurrentValueNameSpace localCurrentValueNameSpace = new CurrentValueNameSpace(paramObject);
    paramCalculator.pushNameSpace(localCurrentValueNameSpace);
    boolean bool = false;
    try
    {
      bool = Boolean.TRUE.equals(paramCalculator.eval(getFormula()));
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    paramCalculator.removeNameSpace(localCurrentValueNameSpace);
    return bool;
  }

  public int[] evalTableData(TableData paramTableData, int[] paramArrayOfInt, int paramInt, Calculator paramCalculator)
  {
    if ((paramTableData == null) || (paramCalculator == null))
      return new int[0];
    int i = 0;
    try
    {
      i = paramTableData.getRowCount();
    }
    catch (TableDataException localTableDataException)
    {
      FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
    }
    if (paramArrayOfInt == null)
      paramArrayOfInt = IntList.range(i);
    IntList localIntList = new IntList();
    SingleRowNameSpace localSingleRowNameSpace = new SingleRowNameSpace(paramTableData, -2147483648, null);
    paramCalculator.pushNameSpace(localSingleRowNameSpace);
    for (int j = 0; j < paramArrayOfInt.length; ++j)
    {
      int k = paramArrayOfInt[j];
      if (k >= 0)
      {
        if (k >= i)
          break label182:
        try
        {
          localSingleRowNameSpace.setRowIndex(k);
          localSingleRowNameSpace.setCurrentValue(paramTableData.getValueAt(k, paramInt));
          if (Boolean.TRUE.equals(paramCalculator.eval(getFormula())))
            localIntList.add(k);
        }
        catch (Exception localException)
        {
          FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
        }
      }
    }
    label182: paramCalculator.removeNameSpace(localSingleRowNameSpace);
    return localIntList.toArray();
  }

  public String toString()
  {
    return this.formulaContent;
  }

  public boolean equals(Object paramObject)
  {
    if ((paramObject == null) || (!(paramObject instanceof FormulaCondition)))
      return false;
    FormulaCondition localFormulaCondition = (FormulaCondition)paramObject;
    return ComparatorUtils.equals(localFormulaCondition.getFormula(), getFormula());
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if ((paramXMLableReader.isChildNode()) && ("Formula".equals(paramXMLableReader.getTagName())))
    {
      String str = paramXMLableReader.getElementValue();
      if (StringUtils.isNotBlank(str))
        this.formulaContent = ((str.startsWith("=")) ? str.substring(1) : str);
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    if (StringUtils.isNotBlank(this.formulaContent))
      paramXMLPrintWriter.startTAG("Formula").textNode(getFormula()).end();
  }

  public JSONObject createJSON()
    throws Exception
  {
    return null;
  }

  public void parseJSON(JSONObject paramJSONObject)
    throws Exception
  {
  }
}